#include "../lib/csapp.h"
#include "recv_mem.h"
#include "to_client.h"
#include "sort_mem.h"

void *output_mem(){
     int i,j,n;

     while(1){

          n = num;
          for(i=0;i<n;i++){
               printf("NO.%s Worker has %s kb free memory\n",uses[i].ipaddr, uses[i].mems);
          }
          if (num>0) printf("\n");
          usleep(10000000);
     }
}

int main()
{
     memset(uses,0,sizeof(uses));
     num = 0;
     pthread_t clit;
     int cl;
     cl = pthread_create(&clit, NULL, to_client, NULL);

     int server_sockfd,client_sockfd;
     int server_len,client_len;

     struct sockaddr_in server_sockaddr,client_sockaddr;

     server_sockfd = socket(AF_INET,SOCK_STREAM, 0);

     server_sockaddr.sin_family=AF_INET;
     server_sockaddr.sin_port=htons(PORTTW);
     server_sockaddr.sin_addr.s_addr=INADDR_ANY;

     server_len=sizeof(server_sockaddr);

     int j=1;
     setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&j,sizeof(j));

     if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,server_len)==-1){
          perror("bind:");
          exit(1);
     }

     if(listen(server_sockfd,5)==-1){
          perror("listen:");
          exit(1);
     }
     printf("Listening from Worker\n");

     client_len=sizeof(client_sockaddr);

     pthread_t thread[CSIZE];
     int i,ret,temp;
     pthread_t outt;
     temp = pthread_create(&outt , NULL, output_mem, NULL);
     while(1) {
          if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_sockaddr,&client_len))==-1) {
               perror("accept error:");
               exit(1);
          }
          printf("Worker:%s login\n",inet_ntoa(client_sockaddr.sin_addr));
          strcpy(uses[num].ipaddr, inet_ntoa(client_sockaddr.sin_addr));

          struct mypara mp;
          mp.num=num++;
          mp.data=client_sockfd;

          ret = pthread_create(&thread[num],NULL,recv_mem,&(mp));
          usleep(10000);
     }
     close(client_sockfd);
     return 0;
}
